package be.bonamis.umpire.ui.client.mvp.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import be.bonamis.projects.umpire.business.dto.ClubFPScoreDTO;
import be.bonamis.projects.umpire.business.dto.GameDTO;
import be.bonamis.projects.umpire.business.dto.UmpireDTO;
import be.bonamis.umpire.ui.client.mvp.HistoryView;
import be.bonamis.umpire.ui.client.resources.Resources;

import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.IdentityColumn;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.inject.Inject;

public class HistoryViewImpl extends Composite implements HistoryView {
	
	private Presenter presenter;
	private UmpireDTO umpire;
	private List<GameDTO> games;
	private GameDTO game;

	interface HistoryViewImplUiBinder extends UiBinder<HTMLPanel, HistoryViewImpl> {
	}

	@UiField
	Button dispoButton;
	
	@UiField
	Button whoButton;
	
	@UiField
	Button quizButton;
	
	@UiField
	VerticalPanel reportPanel;

	@UiField
	Button validateButton;
	
	@UiField
	CellTable<GameDTO> gamesTable;
	
	@UiField
	CellTable<ClubFPScoreDTO> resultTable;
	
	@UiHandler("dispoButton")
	public void onClickDispo(ClickEvent event) {
		presenter.dispoButtonClicked();
	}
	
	@UiHandler("whoButton")
	public void onClickWho(ClickEvent event) {
		presenter.whoButtonClicked();
	}
	
	@UiHandler("quizButton")
	public void onClickQuiz(ClickEvent event) {
	}
	
	@UiHandler("validateButton")
	void onClickToValidate(ClickEvent event) {
		event.preventDefault();
		presenter.putResponsesInDB(umpire.getId(), game.getId(), reportPanel, panels, tBoxes);		
	}
	
	final List<HorizontalPanel> panels = new ArrayList<HorizontalPanel>();
	final List<TextBox> tBoxes = new ArrayList<TextBox>();
	private ListDataProvider<GameDTO> gamesDataProvider;
	private ListDataProvider<ClubFPScoreDTO> scoresDataProvider;
	
	DateTimeFormat gameDayFormat = DateTimeFormat.getFormat("dd MMMM yyyy");

	FlexTable reportTable;
	final Integer COLUMN_QUESTION_TEAM_HOME = 0;
	final Integer COLUMN_BOX_HOME = 1;
	final Integer COLUMN_BOX_AWAY = 2;
	

	@Inject
	public HistoryViewImpl(HistoryViewImplUiBinder uiBinder, final Resources resources) {
		initWidget(uiBinder.createAndBindUi(this));
		resources.css().ensureInjected();
		postInit();		
		createHistoryTable();
	}

	private void createHistoryTable() {
		DateCell dateCell = new DateCell(gameDayFormat);
		Column<GameDTO, Date> dateColumn = new Column<GameDTO, Date>(dateCell) {
			@Override
			public Date getValue(GameDTO object) {
				return object.getGameDay();
			}
		};		
		
		TextColumn<GameDTO> hClubColumn = new TextColumn<GameDTO>() {
			@Override
			public String getValue(GameDTO dto) {
				return dto.getHomeClub();
			}
		};
		
		TextColumn<GameDTO> aClubColumn = new TextColumn<GameDTO>() {
			@Override
			public String getValue(GameDTO dto) {
				return dto.getAwayClub();
			}
		};
		
		TextColumn<GameDTO> umpire01Column = new TextColumn<GameDTO>() {
			@Override
			public String getValue(GameDTO dto) {
				return dto.getUmpire01();
			}
		};
		
		TextColumn<GameDTO> umpire02Column = new TextColumn<GameDTO>() {
			@Override
			public String getValue(GameDTO dto) {
				return dto.getUmpire02();
			}
		};
		
		ActionCell<GameDTO> participateCell = new ActionCell<GameDTO>("Participate",
				new ActionCell.Delegate<GameDTO>() {
					@Override
					public void execute(GameDTO dto) {
						game = dto;
						gamesTable.setVisible(false);
						reportPanel.setVisible(true);
						reportPanel.add(createCellTable(dto.getHomeClub(),dto.getAwayClub()));
						validateButton.setVisible(true);
					}
				});		
		
		IdentityColumn<GameDTO> particpateCellColumn = new IdentityColumn<GameDTO>(participateCell);		
		
		gamesTable.addColumn(dateColumn);
		gamesTable.addColumn(hClubColumn);
		gamesTable.addColumn(aClubColumn);
		gamesTable.addColumn(umpire01Column);
		gamesTable.addColumn(umpire02Column);
		gamesTable.addColumn(particpateCellColumn);
		
		gamesDataProvider = new ListDataProvider<GameDTO>();
		gamesDataProvider.addDataDisplay(gamesTable);
		scoresDataProvider = new ListDataProvider<ClubFPScoreDTO>();
		scoresDataProvider.addDataDisplay(resultTable);		
	}

	private void postInit() {
		String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
		if (currentLocale.equals("default")) {
			currentLocale = "fr";
		}
		String[] localeNames = LocaleInfo.getAvailableLocaleNames();
		for (String localeName : localeNames) {
			if (!localeName.equals("default")) {
			}
		}
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public Presenter getPresenter() {
		return presenter;
	}

	@Override
	public void setValues(UmpireDTO umpire, List<GameDTO> games) {
		this.umpire = umpire;	
		this.games = games;
		
		gamesDataProvider.getList().clear();
		List<GameDTO> list = gamesDataProvider.getList();
		for (GameDTO dto : games) {
			list.add(dto);
		}		
	}

	@Override
	public UmpireDTO getUmpire() {
		return umpire;
	}
	
	private FlexTable createCellTable(String home, String away) {
		reportTable = new FlexTable();
		int row = 0;
		
		reportTable.setText(row, COLUMN_QUESTION_TEAM_HOME, "Question");
		reportTable.setText(row, COLUMN_BOX_HOME, home);
		reportTable.setText(row, COLUMN_BOX_AWAY, away);
		
		row++;
		reportTable.setText(row, COLUMN_QUESTION_TEAM_HOME, "Accueil des arbitres");
		HorizontalPanel panel =  createRadios(row,"home0");
		panels.add(panel);
		reportTable.setWidget(row, COLUMN_BOX_HOME,panel);
		
		panel =  createRadios(row,"away0");
		panels.add(panel);
		reportTable.setWidget(row, COLUMN_BOX_AWAY, panel);
		
		row++;
		reportTable.setText(row, COLUMN_QUESTION_TEAM_HOME, "Attitude / Comportement des joueurs /équipe");
		TextBox widget01 = new TextBox();
		tBoxes.add(widget01);
		reportTable.setWidget(row, COLUMN_BOX_HOME, widget01);
		TextBox widget02 = new TextBox();
		tBoxes.add(widget02);
		reportTable.setWidget(row, COLUMN_BOX_AWAY, widget02);
		
		row++;
		reportTable.setText(row, COLUMN_QUESTION_TEAM_HOME, "Line up");
		panel =  createRadios(row,"home0");
		panels.add(panel);
		reportTable.setWidget(row, COLUMN_BOX_HOME, panel);
		panel =  createRadios(row,"away0");
		panels.add(panel);
		reportTable.setWidget(row, COLUMN_BOX_AWAY, panel);		
		
		return reportTable;
	}
	
	private HorizontalPanel createRadios(int row, String homeaway) {
		HorizontalPanel panel01 = new HorizontalPanel();
		panel01.add(new RadioButton(homeaway + row, "Oui"));
		panel01.add(new RadioButton(homeaway + row, "Non"));
		return panel01;
	}

	@Override
	public void updateScores(List<ClubFPScoreDTO> list) {
		gamesTable.setVisible(false);
		reportTable.setVisible(true);
		List<ClubFPScoreDTO> scores = scoresDataProvider.getList();
		for (ClubFPScoreDTO clubFPScoreDTO : list) {
			scores.add(clubFPScoreDTO);
		}		
	}
}
